{include file="public/header"}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list layui-count" data-type="add">添加</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="table-content-list">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="insert"><i class="layui-icon layui-icon-add-1"></i></a>
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update"><i class="layui-icon layui-icon-edit"></i></a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></a>
      </script>
    </div>
  </div>
</div>
<script>
  layui.use(['layer', 'table', 'form'], function(){
  var $ = layui.$
          ,table = layui.table
          ,form = layui.form;
  table.render({
    elem: '#LAY-app-content-list'
    ,url: "{:url('systemMenu/getList')}"
    , page: false
    , cellMinWidth: 120
    ,cols: [[
      {field: 'sort',title: '排序', edit: 'text', width: 80, align: 'center' }
      ,{field: 'name', title: '名称', templet: function(d){
          return '<span style="font-weight:500;">'+ d.str +'</span>' +d.name;
        }, width: 300}
      , {field: 'id', title: 'ID', align:'center', width: 100}
      ,{field: 'url', title: 'url', templet: function(d){
          if (d.controller != '' && d.function != '') {
            return d.module + '/' + d.controller + '/' + d.function;
          } else{
            return '';
          }
        }, align: 'center', width: 200}
      ,{field: 'is_display', title: '状态', templet: function (d) {
          if (d.is_display == 1) {
            return "左侧菜单";
          } else {
            return "操作节点";
          }
        }, align: 'center'}
      ,{field: 'update_time', title: '最后更新时间', align:'center', sort: true}
      ,{title: '操作', minWidth: 150, align: 'center', toolbar: '#table-content-list'}
    ]]
    ,text: {
      none: '暂无相关数据'
    }
  });
  // 监听单元格编辑
  table.on('edit(LAY-app-content-list)', function(obj){
    var value = obj.value
            ,data = obj.data
            ,field = obj.field;
    $.ajax({
      url:"{:url('systemMenu/sort')}",
      type:'post',
      data:{'id':data.id, sort:value},
      success: function(res){
        if(res.code == 0){
          layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
        } else {
          layer.msg(res.msg,{icon:5});
        }
      }
    });
  });

  // 监听工具条，新增，编辑（查看），删除
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'delete'){
      layer.confirm('确定删除该条记录吗？', {title : '温馨提示'}, function(index){
        $.ajax({
          url:"{:url('SystemMenu/delete')}",
          type:'post',
          data:{'id':data.id},
          success: function(res){
            if(res.code == 0){
              obj.del();
              layer.msg(res.msg,{icon:1});
            } else {
              layer.msg(res.msg,{icon:5});
            }
          }
        });
      });
    } else if(obj.event === 'update'){
      layer.open({
        type: 2
        ,title: '编辑菜单'
        ,content: "{:url('SystemMenu/edit')}?id="+ data.id
        ,maxmin: true
        ,area: ['700px', '80%']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
                  ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
          // 监听提交 layuiadmin-app-form-edit
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field;
            $.ajax({
              url:"{:url('SystemMenu/edit')}",
              type:'post',
              data:{'field':field},
              success:function(res){
                if(res.code == 0){
                  layer.msg(res.msg,{icon:1});
                  table.reload('LAY-app-content-list');
                  layer.close(index);
                } else{
                  layer.msg(res.msg,{icon:5});
                  return false;
                }
              }
            });
          });
          submit.trigger('click');
        }
      });
    } else if(obj.event === 'insert'){
      layer.open({
        type: 2
        ,title: '新增菜单'
        ,content: "{:url('SystemMenu/insert')}?pid=" + data.id
        ,maxmin: true
        ,area: ['700px', '560px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
                  ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
          //监听提交layuiadmin-app-form-edit
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            $.ajax({
              url:"{:url('SystemMenu/insert')}",
              type:'post',
              data:{'field':field},
              success:function(res){
                if(res.code == 0){
                  layer.msg(res.msg,{icon:1});
                  table.reload('LAY-app-content-list');
                  layer.close(index); //关闭弹层
                } else{
                  layer.msg(res.msg,{icon:5});
                }
              }
            });
          });
          submit.trigger('click');
        }
      });
    }
  });

  var $ = layui.$, active = {
    add: function(){
      layer.open({
        type: 2
        ,title: '添加菜单'
        ,content: "{:url('SystemMenu/insert')}"
        ,maxmin: true
        ,area: ['700px', '560px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
                  ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
          //监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field;
            if(field.name == ''){ layer.msg('菜单名称不能为空',{icon:5});}
            $.ajax({
              url:"{:url('SystemMenu/insert')}",
              type:'post',
              data:{'field':field},
              success:function(res){
                if(res.code == 0){
                  layer.msg(res.msg,{icon:1});
                  table.reload('LAY-app-content-list');
                  layer.close(index);
                } else {
                  layer.msg(res.msg,{icon:5});
                }
              }
            });
          });
          submit.trigger('click');
        }
      });
    },
  };
  //监听删除，添加
  $('.layui-btn.layuiadmin-btn-list').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>
{include file="public/footer"}
